<?
class chequera{

	// Propiedades
	var $id;
	var $id_zona;
	var $id_banco;
	var $nombre_banco;
	var $id_cuenta;
	var $cuenta;
	var $incio;
	var $final;
	var $actual;
	var $estatus;
	var $total;

	function get($conn, $id){
		try {
			$q = "SELECT A.*, B.numero AS cuenta, C.id AS id_banco, C.nombre AS nombre_banco, B.id_zona FROM chequeras AS A INNER JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id INNER JOIN bancos AS C ON B.id_banco = C.id WHERE A.id=$id";
			//die($q);
			$r = $conn->Execute(utf8_encode($q));
			if(!$r->EOF){
				$this->id = $r->fields['id'];
				$this->id_zona = $r->fields['id_zona'];
				$this->id_banco = $r->fields['id_banco'];
				$this->nombre_banco = $r->fields['nombre_banco'];
				$this->id_cuenta = $r->fields['id_cuenta'];
				$this->cuenta = $r->fields['cuenta'];
				$this->inicio = $r->fields['inicio'];
				$this->final = $r->fields['final'];
				$this->actual = $r->fields['actual'];
				$this->estatus = $r->fields['estatus'];
			}
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function get_all($conn, $orden="C.nombre, B.numero, A.inicio ", $Tipo, $Valor, $TipoE, $max=10, $from=1){
		try {
			$id_zona = $_SESSION['SesionZona'];
			$TipoE = empty($TipoE) ? 0 : $TipoE;
			
			if(empty($Valor)){
				$q = "SELECT A.id FROM chequeras AS A INNER JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id INNER JOIN bancos AS C ON B.id_banco = C.id ";
				$q.= "WHERE B.id_zona = $id_zona ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==0){
				$q = "SELECT A.id FROM chequeras AS A INNER JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id INNER JOIN bancos AS C ON B.id_banco = C.id ";
				$q.= "WHERE B.id_zona = $id_zona  AND A.id::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			//	die($q);			
			}
			elseif($Tipo==1){
				$q = "SELECT A.id FROM chequeras AS A INNER JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id INNER JOIN bancos AS C ON B.id_banco = C.id ";
				$q.= "WHERE B.id_zona = $id_zona  AND C.nombre::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==2){
				$q = "SELECT A.id FROM chequeras AS A INNER JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id INNER JOIN bancos AS C ON B.id_banco = C.id ";
				$q.= "WHERE B.id_zona = $id_zona  AND B.numero::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==3){
				$q = "SELECT A.id FROM chequeras AS A INNER JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id INNER JOIN bancos AS C ON B.id_banco = C.id ";
				$q.= "WHERE B.id_zona = $id_zona  AND A.inicio::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==4){
				$q = "SELECT A.id FROM chequeras AS A INNER JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id INNER JOIN bancos AS C ON B.id_banco = C.id ";
				$q.= "WHERE B.id_zona = $id_zona  AND A.final::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==5){
				$q = "SELECT A.id FROM chequeras AS A INNER JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id INNER JOIN bancos AS C ON B.id_banco = C.id ";
				$q.= "WHERE B.id_zona = $id_zona  AND A.actual::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}
			//die($q);			
			$r = ($max!=0) ? $conn->SelectLimit($q, $max, $from) : $conn->Execute(utf8_encode($q));
			$coleccion=array();
			while(!$r->EOF){
				$ue = new chequera;
				$ue->get($conn, $r->fields['id']);
				$coleccion[] = $ue;
				$r->movenext();
			}
			$this->total = $r->RecordCount();
			return $coleccion;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function add($conn, $id_cuenta, $inicio, $final, $actual, $estatus){
		try {

			$q = "INSERT INTO chequeras ";
			$q.= "(id_cuenta, inicio, final,actual, estatus) ";
			$q.= "VALUES ";
			$q.= "($id_cuenta, $inicio, $final, $actual, upper('$estatus')) ";
			//die($q);
			$conn->Execute(utf8_encode($q));
			
			$this->auditoria($conn, $q);
			
			return REG_ADD_OK;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function set($conn, $id, $id_cuenta, $inicio, $final, $actual, $estatus){
		try {
			$q = "UPDATE chequeras SET id_cuenta=$id_cuenta, inicio=$inicio, final=$final, actual=$actual, estatus=upper('$estatus') ";
			$q.= "WHERE id=$id";	
			//die($q);
			$conn->Execute(utf8_encode($q));
			
			$this->auditoria($conn, $q);
			
			return REG_SET_OK;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function del($conn, $id){
		try {
			$q = "DELETE FROM chequeras WHERE id=$id";
			//die($q);
			$conn->Execute(utf8_encode($q));
			
			$this->auditoria($conn, $q);
			
			return REG_DEL_OK;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}
	
	function total_registro_busqueda($conn,$Tipo, $Valor, $TipoE, $orden="A.id")
	{
		$id_zona = $_SESSION['SesionZona'];
		$TipoE = empty($TipoE) ? 0 : $TipoE;
		if(empty($Valor)){
			$q = "SELECT count(*) AS total FROM chequeras AS A INNER JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id INNER JOIN bancos AS C ON B.id_banco = C.id  WHERE B.id_zona = $id_zona";
		}
		elseif($Tipo==0){
			$q = "SELECT count(*) AS total FROM chequeras AS A INNER JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id INNER JOIN bancos AS C ON B.id_banco = C.id ";
			$q.= " WHERE B.id_zona = $id_zona  AND id::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==1){
			$q = "SELECT count(*) AS total FROM chequeras AS A INNER JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id INNER JOIN bancos AS C ON B.id_banco = C.id  ";
			$q.= " WHERE B.id_zona = $id_zona  AND B.nombre::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==2){
			$q = "SELECT count(*) AS total FROM chequeras AS A INNER JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id INNER JOIN bancos AS C ON B.id_banco = C.id ";
			$q.= " WHERE B.id_zona = $id_zona  AND A.numero::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==3){
			$q = "SELECT count(*) AS total FROM chequeras AS A INNER JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id INNER JOIN bancos AS C ON B.id_banco = C.id ";
			$q.= " WHERE B.id_zona = $id_zona  AND A.numero::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==4){
			$q = "SELECT count(*) AS total FROM chequeras AS A INNER JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id INNER JOIN bancos AS C ON B.id_banco = C.id ";
			$q.= " WHERE B.id_zona = $id_zona  AND A.numero::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==5){
			$q = "SELECT count(*) AS total FROM chequeras AS A INNER JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id INNER JOIN bancos AS C ON B.id_banco = C.id ";
			$q.= " WHERE B.id_zona = $id_zona  AND A.numero::text ILIKE '%$Valor%'::text";
		}
		$r = $conn->Execute(utf8_encode($q));
		$total = $r->fields['total'];
		return $total;
	}

	function auditoria($conn, $query) {
		$id_usuario = $_SESSION['id'];
		$q = "INSERT INTO auditoria(id_usuario, query) VALUES($id_usuario,$$" . $query . "$$);";
		//die($q);
		$conn->Execute(utf8_encode($q));

		return true;
	}			

}
?>
